package com.loto.insurance.oem.web.controller;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.oem.web.entity.CustFeeFlow;
import com.loto.insurance.oem.web.entity.ProductInfo;
import com.loto.insurance.oem.web.entity.ajax.BaseJson;
import com.loto.insurance.oem.web.entity.config.OEMConfig;
import com.loto.insurance.oem.web.enumeration.ConfigFileEnum;
import com.loto.insurance.oem.web.service.CustInfoService;
import com.loto.insurance.oem.web.service.OemService;
import com.loto.insurance.oem.web.service.ProductInfoService;
import com.loto.insurance.oem.web.utils.Config;
import com.loto.utils.XML;

@Controller
public class WebserviceController {
  static Logger logger = LogManager.getLogger(WebserviceController.class);

  @Autowired
  ProductInfoService productInfoService;

  @Autowired
  OemService oemService;

  @Autowired
  CustInfoService custInfoService;

  // 创建产品
  @RequestMapping(value = "/webservice/createproduct.service", method = RequestMethod.POST)
  public ModelAndView createproduct(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin, @RequestParam("content") String content,
	  HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	OEMConfig configFile = Config.getConfigFile(request,
	    ConfigFileEnum.OEMCONFIG, OEMConfig.class);
	if (configFile.getOemAcc().equals(acc)
	    && configFile.getOemPin().equals(pin)) {
	  ProductInfo productInfo = XML.fromXML(content, ProductInfo.class);
	  if (productInfo != null) {
		if (productInfoService.insertProduct(productInfo) > 0) {
		  logger.info("大后台通知创建产品成功：" + productInfo.getProductId() + "-"
			  + productInfo.getProductName());
		  baseJson.setOk(true);
		} else
		  logger.error("大后台通知创建产品失败：" + productInfo.getProductId() + "-"
			  + productInfo.getProductName());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 修改产品
  @RequestMapping(value = "/webservice/updateproduct.service", method = RequestMethod.POST)
  public ModelAndView updateproduct(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin, @RequestParam("content") String content,
	  HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	OEMConfig configFile = Config.getConfigFile(request,
	    ConfigFileEnum.OEMCONFIG, OEMConfig.class);
	if (configFile.getOemAcc().equals(acc)
	    && configFile.getOemPin().equals(pin)) {
	  ProductInfo productInfo = XML.fromXML(content, ProductInfo.class);
	  if (productInfo != null) {
		if (productInfoService.updateProduct(productInfo) > 0) {
		  logger.info("大后台通知修改产品成功：" + productInfo.getProductId() + "-"
			  + productInfo.getProductName());
		  baseJson.setOk(true);
		} else
		  logger.error("大后台通知修改产品失败：" + productInfo.getProductId() + "-"
			  + productInfo.getProductName());
	  }
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  @RequestMapping(value = "/webservice/removeproduct.service", method = RequestMethod.POST)
  public ModelAndView removeproduct(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin,
	  @RequestParam("productId") long productId, HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	OEMConfig configFile = Config.getConfigFile(request,
	    ConfigFileEnum.OEMCONFIG, OEMConfig.class);
	if (configFile.getOemAcc().equals(acc)
	    && configFile.getOemPin().equals(pin)) {
	  if (productInfoService.deleteProduct(productId) > 0) {
		logger.info("大后台通知删除产品成功：" + productId);
		baseJson.setOk(true);
	  } else
		logger.error("大后台通知删除产品失败：" + productId);
	}
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 更新OEM信息
  @RequestMapping(value = "/webservice/updateoeminfo.service", method = RequestMethod.POST)
  public ModelAndView updateoeminfo(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin, @RequestParam("content") String content,
	  HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);
	/*
	 * OEMConfig configFile = Config.getConfigFile(request,
	 * ConfigFileEnum.OEMCONFIG, OEMConfig.class); if
	 * (configFile.getOemAcc().equals(acc) &&
	 * configFile.getOemPin().equals(pin)) {
	 */
	/*
	 * StringBuilder jdbcContent = new StringBuilder();
	 * jdbcContent.append("driver=com.mysql.jdbc.Driver\r\n");
	 * jdbcContent.append("url=" + configFile.getOemDbConnect() + "\r\n");
	 * jdbcContent.append("username=" + configFile.getOemDbUsername() + "\r\n");
	 * jdbcContent.append("password=" + configFile.getOemDbPassword());
	 */
	OEMConfig config = XML.fromXML(content, OEMConfig.class);
	if (config != null
	    && Config.updateConfigFile(request, ConfigFileEnum.OEMCONFIG, config,
	        OEMConfig.class)) {
	  /*
	   * && Config.updatePropertiesFile(request, ConfigFileEnum.JDBCCONFIG,
	   * jdbcContent.toString())) { WebApplicationContext context =
	   * WebApplicationContextUtils
	   * .getWebApplicationContext(request.getSession().getServletContext()); if
	   * (context.getParent() != null) {
	   * ((AbstractRefreshableApplicationContext) context.getParent())
	   * .refresh(); } ((AbstractRefreshableApplicationContext)
	   * context).refresh();
	   * 
	   * PooledDataSource dataSource = (PooledDataSource) context
	   * .getBean("dataSource"); dataSource.setDriver("com.mysql.jdbc.Driver");
	   * dataSource.setUrl(configFile.getOemDbConnect() + ";user=" +
	   * configFile.getOemDbUsername() + ";password=" +
	   * configFile.getOemDbPassword());
	   */

	  logger.info("大后台通知更新OEM信息成功");
	  baseJson.setOk(true);
	} else
	  logger.error("大后台通知更新OEM信息失败");
	// }
	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 查询OEMadmin余额
  @RequestMapping(value = "/webservice/queryadminusefulfee.service", method = RequestMethod.POST)
  public ModelAndView queryadminusefulfee(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin, HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	OEMConfig configFile = Config.getConfigFile(request,
	    ConfigFileEnum.OEMCONFIG, OEMConfig.class);
	if (configFile.getOemAcc().equals(acc)
	    && configFile.getOemPin().equals(pin)) {
	  BigDecimal usefulfee = custInfoService.findUsefulfeeByName("admin");
	  baseJson.setMsg(String.valueOf(usefulfee));
	  logger.info("大后台获取admin余额成功:" + usefulfee);
	  baseJson.setOk(true);
	} else
	  logger.error("大后台获取admin余额失败");

	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }

  // 更新OEMadmin余额
  @RequestMapping(value = "/webservice/adjustadminfee.service", method = RequestMethod.POST)
  public ModelAndView adjustfee(@RequestParam("acc") String acc,
	  @RequestParam("pin") String pin, @RequestParam("content") String content,
	  HttpServletRequest request) {
	BaseJson baseJson = new BaseJson();
	baseJson.setOk(false);

	OEMConfig configFile = Config.getConfigFile(request,
	    ConfigFileEnum.OEMCONFIG, OEMConfig.class);
	if (configFile.getOemAcc().equals(acc)
	    && configFile.getOemPin().equals(pin)) {
	  CustFeeFlow feeFlow = XML.fromXML(content, CustFeeFlow.class);
	  if (feeFlow != null && custInfoService.adjustFee(feeFlow)) {
		logger.info("大后台通知更新admin余额成功:" + feeFlow.getFlowChangedFee());
		baseJson.setOk(true);
	  } else {
		logger.error("大后台通知更新admin余额失败");
	  }
	} else
	  logger.error("大后台通知更新admin余额失败");

	ModelAndView mav = new ModelAndView("result");
	mav.addObject("result", baseJson);
	return mav;
  }
}
